<?php


function getUniqueCode($length = "")
{
	$code = md5(uniqid(rand(), true));
	if ($length != "")
	return substr($code, 0, $length);
	else
	return $code;
}
function ago($timestamp)
{
   $difference = time() - $timestamp;
   if($difference < 86400 && $difference > 0)
   {
	   $periods = array("sec", "m", "hr", "day", "week", "month", "years", "decade");
	   //$periods = array("second", "ms", "hour", "day", "week", "month", "years", "decade");
	   $lengths = array("60","60","24","7","4.35","12","10");
	   for($j = 0; $difference >= $lengths[$j]; $j++)
	   $difference /= $lengths[$j];
	   $difference = round($difference);
	   if($difference != 1) $periods[$j].= "s";
	   $text = "$difference $periods[$j] ago";
   }
   else
   {
   		$text = date('M d y',$timestamp);
   }
   return $text;
}

function removeSpecialCharacters($description, $length = "")
{
	if(empty($length))
		$desc = mb_convert_encoding(stripslashes($description), 'UTF-8', 'UTF-8');
	else
		$desc = mb_convert_encoding(substr(stripslashes($description), 0, $length), 'UTF-8', 'UTF-8');
	
	return $desc;
}

function createthumb( $src_filename, $dst_filename, $max_width, $max_height )
{
       // Get information about the image
    list($src_width, $src_height, $type, $attr) = getimagesize( $src_filename );
	
	// Load the image based on filetype
	switch( $type ) {
	case IMAGETYPE_JPEG:
	$src_img = imagecreatefromjpeg( $src_filename );
	break;
	case IMAGETYPE_PNG:
	$src_img = imagecreatefrompng( $src_filename );
	break;
	case IMAGETYPE_GIF:
	$src_img = imagecreatefromgif( $src_filename );
	break;
	default:
	return false;
	}
	// Did the image fail to load?
	if ( !$src_img ) {
	return false;
	}
	
	// Calculate the output size based on the original's aspect ratio
	if ( $src_width / $src_height > $max_width / $max_height ) {
	$thumb_w = $max_width;
	$thumb_h = $src_height * $max_width / $src_width;
	}
	else {
	$thumb_w = $src_width * $max_height / $src_height;
	$thumb_h = $max_height;
	}
	
	// Create the output image and copy to source to it
	$dst_img = ImageCreateTrueColor( $thumb_w, $thumb_h );
	imagecopyresampled( $dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $src_width, $src_height );
	
	
	// Get information about the output image
	$path_info = pathinfo($dst_filename);
	
	// Setup default return info
	$return = true;
	
	// Load the image based on filetype
	switch ( strtolower( $path_info['extension'] ) ) {
	case 'jpg':
	case 'jpeg':
	imagejpeg( $dst_img, $dst_filename );
	break;
	case 'png':
	imagepng( $dst_img, $dst_filename );
	break;
	case 'gif':
	imagegif( $dst_img, $dst_filename );
	break;
	default:
	$return = $false;
	}
	
	// Clean up memory
	imagedestroy( $dst_img );
	imagedestroy( $src_img );
	
	return $return;
}
	function uploadDocument($docs, $html, $originalFileName,$temporaryFileLocation) {
		
		$fileToUpload = $originalFileName;
		
		$extensions = array(
		'DOC'=>'application/msword',
		'TXT'=>'text/plain',
		'PPT'=>'application/vnd.ms-powerpoint',	
		'PDF'=>'application/pdf',	     
		'XLS'=>'application/vnd.ms-excel');
		
		$ext = explode('.',$originalFileName);
		$ext = strtoupper($ext['1']);
		$mimetype = $extensions[$ext]; 
		
		/*  $filenameParts = explode('.', $fileToUpload);
		$fileExtension = end($filenameParts);*/
		
		if ($temporaryFileLocation) {
		$fileToUpload = $temporaryFileLocation;
		}
		
		//Zend_Gdata_Docs::lookupMimeType($fileExtension)
		
		$newDocumentEntry = $docs->uploadFile($fileToUpload, $originalFileName,
		$mimetype, Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI); 
	}
function changeToGMT($inputGMT,$timeDiff)
{
	$strtime = strtotime($inputGMT);
	return date('Y-m-d H:i:s', mktime(date('H',$strtime), date('i',$strtime), date('s',$strtime)-$timeDiff, date("m",$strtime) , date("d",$strtime), date("Y",$strtime)));

}
function GMTToUser($inputDate,$timeDiff)
{

	$strtime = strtotime($inputDate);
	return date('Y-m-d H:i:s', mktime(date('H',$strtime), date('i',$strtime), date('s',$strtime)+$timeDiff, date("m",$strtime) , date("d",$strtime), date("Y",$strtime)));
}